package com.longnuoqi.mydoctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.longnuoqi.mydoctor.adapter.DoctorMoreListAdapter;
import com.longnuoqi.mydoctor.entity.Ask;
import com.longnuoqi.mydoctor.fragment.DoctorMoreListFragment;
import com.longnuoqi.mydoctor.utils.ActivityCollection;
import com.longnuoqi.mydoctor.utils.FileUtil;
import com.longnuoqi.mydoctor.utils.OtherUtil;
import com.yunhe.mydoctor.R;


import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Lenovo on 2016/1/5.
 */
public class DoctorAskMoreActivity extends AppCompatActivity {
    private ImageView back;
    private ListView listView;
    private DoctorMoreListAdapter adapter;
    private static final String url = "http://www.tngou.net/api/ask/classify";

    private static boolean isExit = false;//判断是否退出
    private static boolean hasTask = false;//判断是否是任务
    private Timer timer = new Timer();//声明timer对象，计时器
    private TimerTask timerTask = new TimerTask() {//为timer安排一次执行或重复执行的任务
        @Override
        public void run() {
            isExit = false;
        }
    };

    /*双击退出程序*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(DoctorAskMoreActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                if (!hasTask) {
                    timer.schedule(timerTask, 2000);//安排在指定延迟后执行指定的任务
                }
            } else {
                ActivityCollection.finishAll();
                //回收资源
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_more_layout);
        ActivityCollection.addActivity(this);
        OtherUtil.showProgress(this, "", "正在加载内容……", 4000);
        init();
    }

    private void init() {
        back = (ImageView) findViewById(R.id.doctor_more_back_btn);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        listView = (ListView) findViewById(R.id.doctor_more_listview);
        initListView();
    }

    private void initListView() {
        RequestQueue queue = Volley.newRequestQueue(DoctorAskMoreActivity.this);
        StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                List<Ask> list = FileUtil.jsonAskTypeList(s);
                adapter = new DoctorMoreListAdapter(DoctorAskMoreActivity.this, list);
                listView.setAdapter(adapter);

                Bundle bundle = new Bundle();
                bundle.putInt("id", 1);
                showFragment(bundle);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int askId = (int) (view.findViewById(R.id.doctor_more_lv_txt)).getTag();
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", askId);
                        showFragment(bundle);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        queue.add(request);

    }

    private void showFragment(Bundle bundle) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Fragment fragment = new DoctorMoreListFragment();
        fragment.setArguments(bundle);
        transaction.replace(R.id.doctor_more_framelayout, fragment);
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollection.removeActivity(this);
    }

}
